package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.tradewar.UserTradeWarPatronsData;

import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class UserTradeWarPatronsDaoImpl extends TempDataObjectDao<UserTradeWarPatronsData> {

	public Map<Integer, UserTradeWarPatronsData> getUserTradeWarPatronsDataMap(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_trade_war_patrons where userId=?";
		Map<Integer, UserTradeWarPatronsData> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTradeWarPatronsData data = parse(rs);
				dataMap.put(data.getPatronsId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserTradeWarPatronsData data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_trade_war_patrons(userId,patronsId,battleTimes,buyBattleTimes,randomBattleTimes,totalRandomBattleTimes,"
				+ "restoreRandomBattleTime,modifyTime,randomBattleRestoreTimes,crossBattleTimes,crossBuyBattleTimes,crossRandomBattleTimes," +
				"crossTotalRandomBattleTimes,crossRestoreRandomBattleTime,crossRandomBattleRestoreTimes) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getPatronsId());
			call.setInt(i++, data.getBattleTimes());
			call.setInt(i++, data.getBuyBattleTimes());
			call.setInt(i++, data.getRandomBattleTimes());
			call.setInt(i++, data.getTotalRandomBattleTimes());
			call.setLong(i++, data.getRestoreRandomBattleTime());
			call.setLong(i++, data.getModifyTime());
			call.setInt(i++, data.getRandomBattleRestoreTimes());
			call.setInt(i++, data.getCrossBattleTimes());
			call.setInt(i++, data.getCrossBuyBattleTimes());
			call.setInt(i++, data.getCrossRandomBattleTimes());
			call.setInt(i++, data.getCrossTotalRandomBattleTimes());
			call.setLong(i++, data.getCrossRestoreRandomBattleTime());
			call.setInt(i++, data.getCrossRandomBattleRestoreTimes());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserTradeWarPatronsData data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_trade_war_patrons SET battleTimes=?,buyBattleTimes=?,randomBattleTimes=?,totalRandomBattleTimes=?,"
				+ "restoreRandomBattleTime=?,modifyTime=?,randomBattleRestoreTimes=?,crossBattleTimes=?,crossBuyBattleTimes=?," +
				"crossRandomBattleTimes=?,crossTotalRandomBattleTimes=?,crossRestoreRandomBattleTime=?,crossRandomBattleRestoreTimes=?" +
				" WHERE userId=? and patronsId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getBattleTimes());
			call.setInt(i++, data.getBuyBattleTimes());
			call.setInt(i++, data.getRandomBattleTimes());
			call.setInt(i++, data.getTotalRandomBattleTimes());
			call.setLong(i++, data.getRestoreRandomBattleTime());
			call.setLong(i++, data.getModifyTime());
			call.setInt(i++, data.getRandomBattleRestoreTimes());
			call.setInt(i++, data.getCrossBattleTimes());
			call.setInt(i++, data.getCrossBuyBattleTimes());
			call.setInt(i++, data.getCrossRandomBattleTimes());
			call.setInt(i++, data.getCrossTotalRandomBattleTimes());
			call.setLong(i++, data.getCrossRestoreRandomBattleTime());
			call.setInt(i++, data.getCrossRandomBattleRestoreTimes());
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getPatronsId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserTradeWarPatronsData parseData(ResultSet rs) throws SQLException {
		UserTradeWarPatronsData data = new UserTradeWarPatronsData();
		data.setUserId(rs.getLong("userId"));
		data.setPatronsId(rs.getInt("patronsId"));
		data.setBattleTimes(rs.getInt("battleTimes"));
		data.setBuyBattleTimes(rs.getInt("buyBattleTimes"));
		data.setRandomBattleTimes(rs.getInt("randomBattleTimes"));
		data.setTotalRandomBattleTimes(rs.getInt("totalRandomBattleTimes"));
		data.setRestoreRandomBattleTime(rs.getLong("restoreRandomBattleTime"));
		data.setRandomBattleRestoreTimes(rs.getInt("randomBattleRestoreTimes"));
		data.setModifyTime(rs.getLong("modifyTime"));
		data.setCrossBattleTimes(rs.getInt("crossBattleTimes"));
		data.setCrossBuyBattleTimes(rs.getInt("crossBuyBattleTimes"));
		data.setCrossRandomBattleTimes(rs.getInt("crossRandomBattleTimes"));
		data.setCrossTotalRandomBattleTimes(rs.getInt("crossTotalRandomBattleTimes"));
		data.setCrossRestoreRandomBattleTime(rs.getLong("crossRestoreRandomBattleTime"));
		data.setCrossRandomBattleRestoreTimes(rs.getInt("crossRandomBattleRestoreTimes"));
		return data;
	}
}
